//
//  LFTMCViewController.m
//  lofter-mvpcourse-ios
//
//  Created by undefined-zola on 04/28/2017.
//  Copyright (c) 2017 undefined-zola. All rights reserved.
//

#import "LFTMCViewController.h"
#import "LofterMvpCourseManager.h"
#import "LFTMCHttpManager.h"
#import "SVProgressHUD.h"
#import "UIView+Toast.h"
//#import "NIMKit.h"

#define NIMMyAccount   @"lilei"
#define NIMMyToken     @"123456"
#define NIMChatTarget  @"hanmeimei"

#define LFTTESTACCOUNT @"helloworldtest"
#define LFTTESTTOKEN   @"783d698813d2b4089cc4db85d71c187f"

#define BBB            @"bbb"
#define BBBToken       @"edb2d2453d70f5dab57eb385e7444e7b"

#define TEAMID  @"35241285"

static NSString *const kLFTMCTeamId = @"43709400";


//tid: 39415867
//群主： qatest5 密码: qa1234
//
//管理员： qatest10 密码： qa1234

@interface LFTMCViewController () <LofterMvpCourseDataSource>

@property (weak, nonatomic) IBOutlet UITextField *tokenTextField;
@property (weak, nonatomic) IBOutlet UITextField *mobIdTextField;

@property (weak, nonatomic) IBOutlet UITextField *teamIdTextField;

@end

@implementation LFTMCViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"Test";
    // Do any additional setup after loading the view, typically from a nib.
    
    self.teamIdTextField.text = [NSString stringWithFormat:@"群id：%@", kLFTMCTeamId];
    self.teamIdTextField.enabled = NO;
    if (TARGET_IPHONE_SIMULATOR) {
        self.tokenTextField.text = @"f8c2a21d248071c7835642da9ba7f8e5";
        self.mobIdTextField.text = @"96174722AB1010404594755C8144F589C5CFBAF8850B2E47912C69848AA408CFFBF1873DD5B4233517AE540D499AA447";
    } else {
        self.tokenTextField.text = @"eb8fc9e0af55443d8e37e1d86cbd70e8";
        self.mobIdTextField.text = @"6BC0C3EAFBF748C93E53C715277B9E3AA871C82639849973EFD4D3FA76758963C6DC6CB3551A5020FB1E200FA11CBBD6";
    }
    
    //    [self defaultEnterTeamAction:[UIButton new]];
}
- (IBAction)defaultEnterTeamAction:(id)sender
{
    LofterMvpCourseTeamModel *model = [[LofterMvpCourseTeamModel alloc] initWithTeamId:@"35241285"
                                                                             accountId:@"helloworldtest"
                                                                          accountToken:@"783d698813d2b4089cc4db85d71c187f"
                                                                              nickName:@"helloworldtest"
                                                                           courseTitle:@"test"
                                                                            courseDesc:@"test"
                                                                       courseDetailUrl:@"www.lofter.com"
                                                                             userAgent:nil
                                                                            teamClosed:NO];
    [self enterTeamWithModel:model];
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    [LofterMvpCourseManager clearCache];
    [self.view makeToast:@"clearCache!" duration:2.0 position:CSToastPositionBottom];
}


- (IBAction)enterTeamAction:(UIButton *)sender
{
    NSString *token = self.tokenTextField.text;
    NSString *mobId = self.mobIdTextField.text;
    NSString *teamId = kLFTMCTeamId;
    
    [SVProgressHUD show];
    __weak __typeof(self) weakSelf = self;
    [[LFTMCHttpManager manager] fetchTeamInfo:teamId lofterToken:token lofterMobId:mobId success:^(NSDictionary *dic) {
//        __strong __typeof(weakSelf) strongSelf = weakSelf;
        
        NSArray *teams = [NSArray arrayWithObjects:@"43709400", @"46332582", @"46332574", nil];
        NSInteger statusCode = [dic[@"status"] integerValue];
        if (statusCode > 0) {
            NSInteger teamStatus = [dic[@"teamStatus"] integerValue];
            LofterMvpCourseTeamModel *model =
            [[LofterMvpCourseTeamModel alloc] initWithTeamId:@"43709400"
                                                   accountId:dic[@"accId"]
                                                accountToken:dic[@"token"]
                                                    nickName:dic[@"nickName"]
                                                 courseTitle:dic[@"title"]
                                                  courseDesc:dic[@"desc"]
                                             courseDetailUrl:dic[@"detailUrl"]
                                                   userAgent:@"lofter-iphone 5.8.0"
                                                  teamClosed:teamStatus == 0];
            [LofterMvpCourseManager showWithDataSource:self
                                             groupInfo:model
                                                fromVC:self
                                        showCompletion:^(BOOL isSucc) {
                                            [SVProgressHUD dismiss];
                                            if (isSucc) {
                                                // 进入群组成功
                                            } else {
                                                [self.view makeToast:@"进入课程失败，请稍后重试" duration:2.0 position:CSToastPositionCenter];
                                            }
                                        }];
        } else{
            [SVProgressHUD dismiss];
            if (statusCode < 0) {
                NSString *courseUrl = dic[@"h5Url"];
                if (courseUrl.length == 0) {
                    [self.view makeToast:@"课程访问异常" duration:2.0 position:CSToastPositionCenter];
                } else {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:courseUrl]];
                }
            } else {
                [self.view makeToast:@"当前课程无效！" duration:2.0 position:CSToastPositionCenter];
            }
        }
    } failure:^(NSError *error) {
        [SVProgressHUD dismiss];
        [self.view makeToast:@"网络异常" duration:2.0 position:CSToastPositionCenter];
    }];
}

- (void)enterTeamWithModel:(LofterMvpCourseTeamModel *)model
{
    __weak __typeof(self) weakSelf = self;
    [SVProgressHUD show];
    [LofterMvpCourseManager showWithDataSource:self groupInfo:model fromVC:self showCompletion:^(BOOL showResult) {
        [SVProgressHUD dismiss];
        if (showResult) {
            // 进入群组成功
            
        } else {
            // 进入群组失败，
            [weakSelf.view makeToast:@"登陆失败"
                        duration:2.0
                        position:CSToastPositionBottom];
        }
    }];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - LofterMvpCourseDataSource

- (void)lftmc_muteTeam:(NSString *)teamId mute:(BOOL)isMute completeHandler:(void (^)(BOOL))completeHandler
{
    [[LFTMCHttpManager manager] updateTeamMute:teamId mute:isMute success:^{
        if (completeHandler) {
            completeHandler(YES);
        }
    } failure:^(NSError *error) {
        if (completeHandler) {
            completeHandler(NO);
        }
    }];
}

- (void)lftmc_closeTeam:(NSString *)teamId completeHandler:(void (^)(BOOL))completeHandler
{
    [[LFTMCHttpManager manager] closeTeamOperation:teamId success:^{
        if (completeHandler) {
            completeHandler(YES);
        }
    } failure:^(NSError *error) {
        if (completeHandler) {
            completeHandler(NO);
        }
    }];
}

- (void)lftmc_trackEventID:(NSString *)eventID category:(NSString *)category label:(NSString *)label attributes:(NSDictionary *)attributes {
    NSLog(@"%@ %@", eventID, label);
}

@end
